package nuc.edu.tlias.controller;

import jakarta.servlet.http.HttpServletRequest;
import nuc.edu.tlias.anno.Log;
import nuc.edu.tlias.pojo.Dept;
import nuc.edu.tlias.pojo.Result;
import nuc.edu.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)

    @GetMapping
    public Result list(){
        System.out.println("查询全部数据");
        List<Dept> deptList = deptService.findall();

        return Result.success(deptList);
    }

    //controller接受参数

    //方法一
   /* @DeleteMapping("/depts")
    public Result delect(HttpServletRequest request){

        String idStr = request.getParameter("id");

        int id = Integer.parseInt(idStr);

        deptService.delect(id);


        return Result.success();
    }*/

    @Log
    @DeleteMapping
    public Result delect(/*@RequestParam*/Integer id){
        //RequestParam中required 默认为true，表示必须传参，否则报错

        //如果请求参数名和方法的参数名不一致，则需要使用@RequestParam指定参数名
        //如果请求参数名和方法的参数名一致，则不需要使用@RequestParam

        deptService.delectById(id);


        return Result.success();
    }


    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){

        // System.out.println("添加部门："+dept.getName());

        deptService.add(dept);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getInfo(@PathVariable/*("id")*/ Integer id){

        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return  Result.success();
    }
}
